package com.qf.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.mapper.DtsPermissionMapper;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsPermission;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Ypl
 * @version V1.0
 * @Project September-mavens
 * @Package com.qf.serviceimpl
 * @Description:
 * @Date 2022/10/13 17:25
 */
@SuppressWarnings("all")
@Service
public class DtsRoleServiceImpl implements DtsRoleService {

    @Autowired
    DtsRoleMapper dtsRoleMapper;
    @Autowired
    DtsAdminMapper dtsAdminMapper;

    @Autowired
    DtsPermissionMapper dtsPermissionMapper;

    /**
     * 根据角色ID查找
     * @param roleIds
     * @return
     */
    @Override
    public Set<String> findRoleByIds(Integer[] roleIds) {

        LambdaQueryWrapper<DtsRole> wrapper = new LambdaQueryWrapper<>();

        wrapper.in(DtsRole::getId, roleIds);
        wrapper.in(DtsRole::getDeleted,false);
        List<DtsRole> roleList = dtsRoleMapper.selectList(wrapper);
        //list转为set  map中就是要返回set中所需要的值
        Set<String> collect = roleList.stream().map(role -> {
            return role.getName();
        }).collect(Collectors.toSet());

        return collect;
    }

    /**
     * 查询所有角色
     * @return
     */
    @Override
    public List<DtsRole> finAllRole() {
        return dtsRoleMapper.selectList(null);
    }

    /**
     * 分页查找
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param username
     * @return
     */
    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, String username) {
        //分页的对象
        IPage<DtsRole> iPage = new Page<>();

        QueryWrapper<DtsRole> wrapper = new QueryWrapper<>();
        //名字查询判断
        if(!StringUtils.isEmpty(username)){
            wrapper.like("name",username);
        }
        //排序规则判断
        if("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else{
            wrapper.orderByAsc(sort);
        }
        dtsRoleMapper.selectPage(iPage,wrapper);

        Map<String,Object> data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());

        return data;
    }

    /**
     * 新增角色
     * @param dtsRole
     */
    @Override
    public void insertRole(DtsRole dtsRole) {
        dtsRole.setDeleted(false);
        dtsRole.setAddTime(new Date());
        dtsRole.setUpdateTime(new Date());
        dtsRoleMapper.insert(dtsRole);
    }

    /**
     * 更新角色
     * @param dtsRole
     */
    @Override
    public void update(DtsRole dtsRole) {
        dtsRole.setDeleted(false);
        dtsRoleMapper.updateById(dtsRole);
    }

    /**
     * 逻辑删除
     * @param dtsRole
     */
    @Override
    public void deleteRole(DtsRole dtsRole) {

        //查询角色是否被管理员关联
        LambdaQueryWrapper<DtsAdmin> adminWrapper = new LambdaQueryWrapper<>();
        adminWrapper.eq(DtsAdmin::getDeleted,false);
        adminWrapper.select(DtsAdmin::getRoleIds);

        List<DtsAdmin> dtsAdmins = dtsAdminMapper.selectList(adminWrapper);
        //遍历所查询到的管理员
        for (DtsAdmin dtsAdmin : dtsAdmins) {
            Integer[] roleIds = dtsAdmin.getRoleIds();
            for (Integer roleId : roleIds) {
                //被关联
                if(roleId.equals(dtsRole.getId())){
                    throw new RuntimeException("该角色已被管理员关联，不可删除！！");
                }
            }
        }

        //判断是否被权限表关联
        LambdaQueryWrapper<DtsPermission> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(DtsPermission::getRoleId,dtsRole.getId());
        wrapper.eq(DtsPermission::getDeleted,false);

        Integer integer = dtsPermissionMapper.selectCount(wrapper);
        //被关联
        if(integer>0){
            throw new RuntimeException("该角色已关联权限，不可删除！！");
        }

        dtsRole.setDeleted(true);
        dtsRoleMapper.updateById(dtsRole);
    }

    /**
     * 是否为超级管理员
     */
    @Override
    public boolean isSuperAdmin(Integer roleId) {
        DtsRole dtsRole = dtsRoleMapper.selectById(roleId);
        if("超级管理员".equalsIgnoreCase(dtsRole.getName())){
            return true;
        }
        return false;
    }
}
